﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using NVelocity;
using NVelocity.App;
using NVelocity.Runtime;
using System.Collections;
using jmail;


namespace kxcxw.Business
{
    public static class SendMailHelper
    {
        public static string SendMail(string mialAddress, string userId, string activateId, string NickName)
        {
            var objString = new
            {
                UserName = NickName,
                UserID = userId,
                ActivateId = activateId

            };
            string text = GetTemplateHtmlString(System.Web.HttpContext.Current.Server.MapPath("~/Content/"), "mailTemplate.htm", objString, "TemplatePage");
            return SendMail(new string[] { mialAddress }, text, "开心促销网注册激活邮件");
        }

        public static string SendMail(string[] toAddress, string content, string subject)
        {
            try
            {
                Message jmail = new Message();
                jmail.From = "kxcxwcom@qq.com";
                jmail.FromName = "开心促销网";
                jmail.MailServerUserName = "kxcxwcom";
                jmail.MailServerPassWord = "kxcxwcom";
                jmail.Subject = subject;
                jmail.HTMLBody = content;
                jmail.Charset = "gb2312";
                jmail.ContentTransferEncoding = "base64";
                jmail.Encoding = "base64";
                jmail.ISOEncodeHeaders = false;

                foreach (string toemail in toAddress)
                {
                    jmail.AddRecipient(toemail, null, null);
                    jmail.Send("smtp.qq.com", false);
                    jmail.ClearRecipients();
                }
                return "true";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }


            //Dimac.JMail.Message msg = new Dimac.JMail.Message();
            //// 发件人
            //msg.From.Email = "qijinwen888@qq.com";
            ////显示姓名
            //msg.From.FullName = "开心促销网";
            ////邮件主题
            //msg.Subject = subject;
            ////邮件内容
            //msg.BodyHtml = content;
            ////字符集.这里设置为GB2312 不设置收到邮件之后中文显示乱码
            //msg.Charset = Encoding.GetEncoding("GB2312");
            ////附件
            ////msg.Attachments.Add(@txtAttach.Text);

            //try
            //{
            //    // 设置SMTP
            //    Smtp smt = new Smtp();
            //    smt.UserName = "qijinwen888"; //your username
            //    smt.Password = "qepwqcong1314";     //your password
            //    smt.HostName = "smtp.qq.com";    //SMTP服务器
            //    smt.Domain = "mail.qq.com";    //主机名
            //    smt.Port = short.Parse("25");   //端口
            //    smt.Authentication = SmtpAuthentication.Login;  //！！验证方式
            //    //收件人
            //    foreach (string toemail in toAddress)
            //    {
            //        msg.To.Clear();
            //        msg.To.Add(toemail);
            //        smt.Send(msg);
            //    }
            //    return "true";
            //}
            //catch (Exception ex)
            //{
            //    return ex.Message;
            //}
        }

        public static string SendEmailToOurWorker(string content, string subject)
        {
            string[] OurWorkersEmail = { "qijinwen2005@qq.com", "57591465@qq.com", "lihanzhen309@qq.com", "409067117@qq.com", "1282776797@qq.com", "510826742@qq.com" };
            return SendMail(OurWorkersEmail, content, subject);
        }

        public static string GetTemplateHtmlString(string TempPath, string FileName, object templatePage, string NameSpace)
        {
            try
            {
                VelocityEngine vltEngine = new VelocityEngine();
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TempPath);
                vltEngine.Init();
                Hashtable modelTable = new Hashtable();
                modelTable.Add(NameSpace, templatePage);
                VelocityContext vltContext = new VelocityContext(modelTable);
                Template vltTemplate = vltEngine.GetTemplate(FileName);
                System.IO.StringWriter vltWriter = new System.IO.StringWriter();
                vltTemplate.Merge(vltContext, vltWriter);
                return vltWriter.GetStringBuilder().ToString();
            }
            catch (Exception ex)
            {

                return "";
            }
        }
    }
}
